package org.luxor.commons.storage.client.qcloud;

import org.luxor.commons.core.utils.StrKit;
import org.luxor.commons.core.utils.StringUtils;
import org.luxor.commons.storage.client.AbstractStorageProperties;
import org.luxor.commons.storage.config.HttpConfig;
import org.luxor.commons.storage.config.ThumbImageConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
 * Qcloud-Client 配置
 *
 * @author Mr.yan @date 2020/2/8
 */
@ConfigurationProperties(prefix = QcloudProperties.PREFIX)
public class QcloudProperties extends AbstractStorageProperties {
    public static final String PREFIX = "spring.storage.qcloud";

    final Boolean DEFAULT_ENABLED = Boolean.FALSE;
    final Integer DEFAULT_CHUNK_SIZE = 5;
    final Integer DEFAULT_DOWNLOAD_CHUNK_SIZE = 100;
    final HttpConfig DEFAULT_HTTP = new HttpConfig();
    final ThumbImageConfig DEFAULT_THUMB_IMAGE = new ThumbImageConfig();

    final String DEFAULT_SECRET_ID = "none";
    final String DEFAULT_SECRET_KEY = "none";
    final Long DEFAULT_APP_ID = 0L;

    final String DEFAULT_BUCKET_NAME = "none";
    final String DEFAULT_REGION = "gz";
    final String DEFAULT_DOMAIN = "https://xxxx.qcloud.com";
    final String DEFAULT_PREFIX = "default";

    /**
     * 是否启用
     */
    private boolean enabled;

    /**
     * 分块大小
     */
    private Integer chunkSize;
    private Integer downloadChunkSize;

    /**
     * 缩略图配置
     */
    @NestedConfigurationProperty
    private ThumbImageConfig thumbImage;

    /**
     * 防盗链配置
     */
    @NestedConfigurationProperty
    private HttpConfig http;

    /**
     * 腾讯云AppId
     */
    private long appId;

    /**
     * 腾讯云ACCESS_KEY_ID
     */
    private String secretId;

    /**
     * 腾讯云ACCESS_KEY_SECRET
     */
    private String secretKey;

    /**
     * 腾讯云存储空间名
     */
    private String bucketName;

    /**
     * Bucket所在的区域：gz-华南  tj-华北  sh-华东
     */
    private String region;

    /**
     * 腾讯云文件路径前缀
     */
    private String prefix;

    /**
     * 腾讯云外链域名
     */
    private String domain;


    public QcloudProperties() {
        super();
        this.enabled = DEFAULT_ENABLED.booleanValue();
        this.chunkSize = DEFAULT_CHUNK_SIZE;
        this.downloadChunkSize = DEFAULT_DOWNLOAD_CHUNK_SIZE;
        this.thumbImage = DEFAULT_THUMB_IMAGE;
        this.http = DEFAULT_HTTP;

        this.secretId = DEFAULT_SECRET_ID;
        this.secretKey = DEFAULT_SECRET_KEY;
        this.appId = DEFAULT_APP_ID;
        this.bucketName = DEFAULT_BUCKET_NAME;
        this.region = DEFAULT_REGION;
        this.prefix = DEFAULT_PREFIX;
        this.domain = DEFAULT_DOMAIN;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public Integer getChunkSize() {
        return chunkSize;
    }

    public void setChunkSize(Integer chunkSize) {
        this.chunkSize = chunkSize;
    }

    public Integer getDownloadChunkSize() {
        return downloadChunkSize;
    }

    public void setDownloadChunkSize(Integer downloadChunkSize) {
        this.downloadChunkSize = downloadChunkSize;
    }

    @Override
    public ThumbImageConfig getThumbImage() {
        return thumbImage;
    }

    public void setThumbImage(ThumbImageConfig thumbImage) {
        this.thumbImage = thumbImage;
    }

    @Override
    public HttpConfig getHttp() {
        return http;
    }

    public void setHttp(HttpConfig http) {
        this.http = http;
    }

    public long getAppId() {
        return appId;
    }

    public void setAppId(long appId) {
        this.appId = appId;
    }

    public String getSecretId() {
        return secretId;
    }

    public void setSecretId(String secretId) {
        this.secretId = secretId;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getBucketName() {
        return bucketName;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getPrefix() {
        return this.prefix;
    }

    public void setPrefix(String prefix) {
        if (!StringUtils.startsWith(prefix, StrKit.SLASH)) {
            prefix = StrKit.SLASH + prefix;
        }
        if (StringUtils.endsWith(prefix, StrKit.SLASH)) {
            prefix = prefix.substring(0, prefix.length() - 1);
        }
        this.prefix = prefix;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        if (StringUtils.endsWith(domain, StrKit.SLASH)) {
            this.domain = domain.substring(0, domain.length() - 1);
        } else {
            this.domain = domain;
        }
    }
}
